var app = getApp();
var wxCharts = require('../../utils/wxcharts.js');
var columnChart = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isMainChartDisplay: true,
    pageIndex: 1,
    totalNumber: '',
    size: 10,
    listCategoryOne: '',
    mychart: '',
    dataLength: '',
    // sort:"",

    ifShownoData: true,
    wrapCategory: false,
    orderStatus: [
      {
        statusName: '全部 ',
        statusCode: '1'
      },
      {
        statusName: '近7天',
        statusCode: '2'
      },
      {
        statusName: '近1个月',
        statusCode: '3'
      },
      {
        statusName: '近3个月',
        statusCode: '4'
      }
    ],
    statusSelect: '1'
  },
  scroll: function(e) {
    var scrollLeft = e.detail.scrollLeft;
    this.setData({ scrollLeft: scrollLeft });
  },
  myquary: function() {
    var that = this;
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getSale', //查询销货报表
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        timeSection: that.data.statusSelect,
        size: that.data.size,
        page: that.data.pageIndex
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 100) {
          var array0 = res.data.result;
          if (that.data.pageIndex != 1) {
            var array1 = that.data.listCategoryOne;
            array1 = array1.concat(array0);
            that.setData({ listCategoryOne: array1 });
          } else {
            that.setData({ listCategoryOne: array0 });
          }
          that.setData({
            totalNumber: res.data.total,
            pageIndex: res.data.page,
            ifShownoData: true
          });
        } else {
          that.setData({ ifShownoData: false });
        }
      },
      fail: function(error) {
        that.setData({ buthiddenNext: true });
      }
    });
  },
  getLocalCity() {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getSaleHistogram', //销货矩形图
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId,
        timeSection: that.data.statusSelect
      },
      success: res => {
        console.log(res.data.result);
        if (res.data.code == 100) {
          var arr1 = [];
          var arr2 = [];
          for (var i = 0; i < res.data.result.length; i++) {
            arr1.push(res.data.result[i].categoryName);
            arr2.push(Math.round(res.data.result[i].totalWeight));
          }
          let simulationData = {
            categories: arr1,
            data: arr2
          };
          that.setData({
            ifShownoData: true,
            dataLength: arr1.length
          });
          that.getWeather(simulationData);
        } else {
          that.setData({ ifShownoData: false });
        }
      }
    });
  },
  getWeather(simulationData) {
    let res = wx.getSystemInfoSync();
    let windowWidth = res.windowWidth;
    console.log(simulationData);

    if (windowWidth < simulationData.categories.length * 70) {
      windowWidth = simulationData.categories.length * 70;
    }
    columnChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'column',
      categories: simulationData.categories,
      legend: false,
      animation: true,
      series: [
        {
          name: '成交量',
          data: simulationData.data,
          color: '#73C1FF',
          format: function(val, name) {
            return val.toFixed(0);
          }
        }
      ],
      yAxis: {
        format: function(val) {
          return val;
        },
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getLocalCity();
    this.myquary();
  },
  //
  toPrice: function() {
    wx.navigateTo({
      url: 'priced'
    });
  },
  //一级菜单选择
  orderStatusList: function(e) {
    var that = this;
    that.setData({
      //把选中值放入判断值
      statusSelect: e.currentTarget.dataset.select
    });
    this.getLocalCity();
    this.setData({ pageIndex: 1 });
    this.myquary();
  },
  onPullDownRefresh: function() {
    this.getLocalCity();
    this.setData({ pageIndex: 1 });
    this.myquary();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var pageIndex = that.data.pageIndex;
    if (pageIndex < that.data.totalNumber / that.data.size) {
      pageIndex++;
      that.setData({ pageIndex: pageIndex });
      this.myquary();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
